home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Development Libraries / SGI IRIX 6.2 Development Libraries.iso / relnotes / dmedia_dev / ch09.z / ch09
Encoding:
Text File  |  1997-09-04  |  57.6 KB  |  1,519 lines

  1.  
  2.  
  3.  
  4.                                   - 1 -
  5.  
  6.  
  7.  
  8.        9.  _A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y
  9.  
  10.        This chapter lists information about the Audio File Library
  11.        programming interface included in the the IRIS Digital Media
  12.        Development Environment.  It includes changes, additions,
  13.        and bug fixes since the last release, and known problems,
  14.        workarounds, and caveats.
  15.  
  16.        The new version of the Audio File Library consists of
  17.        approximately 90 C routines that allow application programs
  18.        to read/write audio sample data and auxiliary header
  19.        information to/from a number of standard audio file formats.
  20.        The library requires no special audio hardware at runtime,
  21.        and is useful for sound file import/export/conversion
  22.        operations as well as real-time recording/playback.
  23.  
  24.        The Audio File Library API allows you to access audio
  25.        samples and nonaudio information from audio files using a
  26.        common set of routines.  The API includes functions for
  27.        reading/writing various types of data including author,
  28.        copyright, name, and annotation strings; sample frame marker
  29.        locations; sample configuration parameters and loop points;
  30.        MIDI exclusive data; and application-defined data.
  31.  
  32.        The Audio File Library implements transparent sample data
  33.        format conversion and compression/decompression for several
  34.        different audio file formats.  The following compression
  35.        algorithms are currently supported:
  36.  
  37.           +o MPEG-1 Layers I and II
  38.  
  39.           +o Aware Inc.'s, proprietary MultiRate algorithm
  40.  
  41.           +o CCITT G.711 mu-law and A-law
  42.  
  43.           +o CCITT G.722 ADPCM
  44.  
  45.        Transparent format conversion and compression/decompression
  46.        means that application programs can process all audio files
  47.        as uncompressed data in any format that the application
  48.        desires.  There is no need to write special code for
  49.        handling different sample formats or types of compressed
  50.        data.  Compressed and decompressed files look the same to
  51.        applications as long as the compression format is one of the
  52.        supported types listed above.
  53.  
  54.        Note:  The MPEG-1 encoder cannot be activated by an
  55.               application without a FlexLM  node-locked MPEG
  56.               Encoder license, available from SGI.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                                   - 2 -
  71.  
  72.  
  73.  
  74.        Note:  The Aware MultiRate codec cannot be activated by an
  75.               application without a FlexLM  node-locked license,
  76.               available from SGI.
  77.  
  78.               See the online man page _a_w_a_r_e(_5) for an overview of
  79.               the Aware compression technology available to
  80.               developers and end users. The online man page
  81.               _a_f_I_n_i_t_C_o_m_p_r_e_s_s_i_o_n(3dm) describes the Audio File
  82.               Library interface to reading/writing audio files that
  83.               contain MultiRate or other types of compressed data.
  84.  
  85.        The Audio File Library API is intended to be general enough
  86.        to allow support for additional digital audio file formats
  87.        and compression schemes in the future.  Applications that
  88.        are developed on top of the current library will be able to
  89.        read these additional file and data formats (with at most
  90.        minor code modifications) after simply relinking with a new
  91.        version of the Audio File Library. Existing applications
  92.        will require slight modifications in order to write
  93.        additional file and data formats.
  94.  
  95.        Applications linked with the new Audio File Library no
  96.        longer need to be linked with any other library.  All that
  97.        is needed on the link line is:
  98.  
  99.        ----llllaaaauuuuddddiiiiooooffffiiiilllleeee
  100.  
  101.        The _I_R_I_S _D_i_g_i_t_a_l _M_e_d_i_a _P_r_o_g_r_a_m_m_i_n_g _G_u_i_d_e contains an
  102.        introduction to programming with the Audio File Library,
  103.        along with a description of each of its functions.  Please
  104.        see section "Documentation Errors" for some important notes.
  105.  
  106.        The on-line man page _a_f_I_n_t_r_o(3dm) provides a brief overview
  107.        of the library and a list of the available procedure calls.
  108.  
  109.        The IRIS Audio Utility Library is a small C language
  110.        procedure call library that contains approximately 10
  111.        auxiliary routines for use with the Audio File Library.
  112.  
  113.        Please see the _I_R_I_S _D_i_g_i_t_a_l _M_e_d_i_a _P_r_o_g_r_a_m_m_i_n_g _G_u_i_d_e for an
  114.        introduction to programming with Audio Utility Library and a
  115.        detailed description of its functions.
  116.  
  117.        Currently, the library consists of the function
  118.        _A_U_c_h_e_c_k_l_i_c_e_n_s_e(3dm), which allows an application to query
  119.        license availability for Aware compressor/decompressor
  120.        software on the current host, and several functions that
  121.        implement a generalized parameter-value list object for use
  122.        with the new Audio File Library routines
  123.        _a_f_I_n_i_t_C_o_m_p_r_e_s_s_i_o_n_P_a_r_a_m_s(3dm) and
  124.        _a_f_G_e_t_C_o_m_p_r_e_s_s_i_o_n_P_a_r_a_m_s(3dm).  See the man page _A_U_p_v_n_e_w(3dm)
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                                   - 3 -
  137.  
  138.  
  139.  
  140.        for a description of these routines.
  141.  
  142.        See the source code _a_i_f_c_c_o_n_v_e_r_t._c in the directory
  143.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s for Audio Utility
  144.        programming example.
  145.  
  146.  
  147.        9.1  _E_x_a_m_p_l_e__P_r_o_g_r_a_m_s__i_n__/_u_s_r_/_s_h_a_r_e_/_s_r_c_/_d_m_e_d_i_a_/_a_u_d_i_o
  148.  
  149.        The subsystems _d_m_e_d_i_a__d_e_v._s_r_c._t_o_o_l_s and
  150.        _d_m_e_d_i_a__d_e_v._s_r_c._e_x_a_m_p_l_e_s contain several source code examples
  151.        for programming with the Audio File Library. Executable
  152.        versions of some of these programs are included as standard
  153.        system utilities which can be installed from the
  154.        _d_m_e_d_i_a__e_o_e._s_w._t_o_o_l_s subsystem included with the IRIX 6.2 O/S
  155.        release.  However, as noted in the Changes section below,
  156.        most of these programs are now obsolete and are only
  157.        provided as example code, and their functionality has been
  158.        taken over by other programs.
  159.  
  160.        _p_l_a_y_a_i_f_c(1) and _r_e_c_o_r_d_a_i_f_c(1) are command-line programs for
  161.        playing and recording AIFF-C/AIFF files, respectively.  The
  162.        programs are built on top of the IRIS Audio Library and the
  163.        Audio File Library.  The source code for these programs is
  164.        available in
  165.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/{_p_l_a_y._c,_s_f_r_e_c_o_r_d._c}
  166.  
  167.        _a_i_f_c_i_n_f_o(1) is a command-line program that parses an AIFF-
  168.        C/AIFF input file and prints a description of the data in
  169.        the file, including characteristics of the audio sample
  170.        data, and a summary of the nonaudio data (strings, loop
  171.        points, and so on) found in the various file header fields.
  172.        The source code is installed in
  173.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/_a_i_f_c_i_n_f_o._c.
  174.  
  175.        _a_i_f_f_2_a_i_f_c(1), _a_i_f_c_2_a_i_f_f(1), _a_i_f_c_c_o_m_p_r_e_s_s(1), and
  176.        _a_i_f_c_d_e_c_o_m_p_r_e_s_s(1) are old command-line utility programs for
  177.        converting between AIFF-C and AIFF files, or for converting
  178.        AIFF-C files between compressed and uncompressed data
  179.        formats.  These utilities are all obsolete, as indicated
  180.        below.  The source for the program is the file
  181.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/_a_i_f_c_c_o_n_v_e_r_t._c.
  182.  
  183.        9.2  _C_h_a_n_g_e_s__a_n_d__A_d_d_i_t_i_o_n_s
  184.  
  185.        This section lists changes/additions to the Audio File
  186.        Library and accompanying example source code since its last
  187.        release.
  188.  
  189.           +o The new AF allows transparent conversion of the audio
  190.             data in a file's track into an uncompressed format of
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                                   - 4 -
  203.  
  204.  
  205.  
  206.             the application's choice.  This new format is known as
  207.             the _v_i_r_t_u_a_l format of the data.  The application need
  208.             not concern itself with the data format in the file; it
  209.             can specify the format in which it would like to
  210.             receive the data.  This also works in the other
  211.             direction:  A buffer of data in any uncompressed format
  212.             may be written out to a file containing data in any
  213.             other format (assuming the audio file type specified
  214.             supports that format).  Read the _a_f_I_n_t_r_o(3dm) man page
  215.             for a detailed explanation of this procedure.
  216.  
  217.           +o The programming examples for the Audio File Library
  218.             (and Audio Utility Library) are now located in the
  219.             directories
  220.             /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/{_s_o_u_n_d_c_o_m_m_a_n_d_s,_s_o_u_n_d_p_l_a_y_e_r,_a_u_d_i_o}.
  221.  
  222.           +o The following list of audio utility programs has been
  223.             made obsolete by the new _d_m_c_o_n_v_e_r_t(3dm) and/or
  224.             _d_m_i_n_f_o(3dm) utilities:
  225.  
  226.             aaaaiiiiffffffff2222aaaaiiiiffffcccc,,,, aaaaiiiiffffcccc2222aaaaiiiiffffffff,,,, aaaaiiiiffffccccccccoooommmmpppprrrreeeessssssss,,,, aaaaiiiiffffccccddddeeeeccccoooommmmpppprrrreeeessssssss,,,, aaaaiiiiffffcccciiiinnnnffffoooo
  227.  
  228.             For backwards compatibility, these utilities still
  229.             exist as symbolic links to the new programs.  SGI
  230.             offers no guarantee that this will be continued in
  231.             future releases.
  232.  
  233.        9.3  _P_u_b_l_i_c__F_u_n_c_t_i_o_n__N_a_m_e__C_h_a_n_g_e_s
  234.  
  235.        The new AF has adopted a new standard for the naming of
  236.        publically available routines.  All Audio File Library
  237.        routines now consist of the prefix "af" (in small case
  238.        letters) followed by any number of words describing the
  239.        function, with the first letter of each word capitalized.
  240.        To allow existing code to run when linked against the new
  241.        library, two forms of backwards- compatibility are built
  242.        into the library:
  243.  
  244.           +o The public header file <dmedia/audiofile.h> contains a
  245.             set of #define's which will cause the preprocessor to
  246.             rename each occurrence of the old routine names in
  247.             existing code when and if it is recompiled from source.
  248.  
  249.           +o For code which is not recompiled but which chooses to
  250.             link against the 2.0 version of the library (see the
  251.             section "How Does This DSO Magic Work?" below), a set
  252.             of wrapper functions are contained within the library
  253.             to allow the old routine symbol names to be resolved
  254.             properly.  For ease in debugging and maximum
  255.             efficiency, it is recommended that programs be
  256.             recompiled to allow the redefines to happen, or better
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                                   - 5 -
  269.  
  270.  
  271.  
  272.             yet, rename the function calls using the new scheme.
  273.  
  274.        9.4  _B_u_g__F_i_x_e_s
  275.  
  276.        Although the available functionality is the same, the 6.2
  277.        version of the Audio File Library is a completely new DSO.
  278.        Because of this, it would not make sense to describe its
  279.        "bug fixes" vs. the old libaudiofile.  Future release notes
  280.        will describe fixes for bugs found in the new version.
  281.  
  282.        9.5  _K_n_o_w_n__P_r_o_b_l_e_m_s__a_n_d__W_o_r_k_a_r_o_u_n_d_s
  283.  
  284.        This section lists problems in the new Audio File Library
  285.        software and ways to work around them.
  286.  
  287.           +o AIFF-C comment chunks: the Audio File Library API does
  288.             not yet include routines for reading/writing comment
  289.             chunk data in AIFF-C files.  The library routine
  290.             _a_f_O_p_e_n_F_i_l_e(3dm) allows you to open an AIFF-C file that
  291.             contains a comment chunk, but there is no way to access
  292.             the comment chunk fields through library routines.
  293.  
  294.           +o Apple Computer proprietary compression algorithms:
  295.             several compression algorithms described in the AIFF-C
  296.             specification (ACE2, ACE8, MAC3, MAC6) are Apple
  297.             proprietary and cannot be decoded on IRIS workstations.
  298.             To avoid problems with proprietary or unsupported
  299.             compression algorithms, convert AIFF-C files so that
  300.             they contain uncompressed data before attempting to
  301.             transfer them between an IRIS workstation and another
  302.             audio workstation platform.
  303.  
  304.           +o Aware MultiRate codec:  An application is only allowed
  305.             to have one AFfilehandle open at a time which uses the
  306.             Aware MultiRate or Lossless compressor or decompressor.
  307.             This applies to the application's entire process share
  308.             group.
  309.  
  310.             Note:  This bug does not apply to the Aware MPEG
  311.                    implementation.
  312.  
  313.        9.6  _D_o_c_u_m_e_n_t_a_t_i_o_n__E_r_r_o_r_s
  314.  
  315.        This section lists errors in the Audio File Library
  316.        documentation.
  317.  
  318.           +o The _I_R_I_S _D_i_g_i_t_a_l _M_e_d_i_a _P_r_o_g_r_a_m_m_i_n_g _G_u_i_d_e has not yet
  319.             been updated to reflect the changes and additions made
  320.             in the new version of the AF.  However, the manual
  321.             pages are completely up-to-date and cover every routine
  322.             available in the new AF.
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                                   - 6 -
  335.  
  336.  
  337.  
  338.  
  339.  
  340.        9.7  _A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y_:__D_e_v_e_l_o_p_e_r__N_o_t_e_s
  341.  
  342.        An important warning to developers about using the new Audio
  343.        File Library.  Read this in order to assure that your
  344.        applications continue to work well with the new expanded
  345.        version of the library.
  346.  
  347.        9.7.1  _I_n_t_r_o_d_u_c_t_i_o_n  It has come to our attention that some
  348.        applications which use the Audio File Library are making
  349.        untrue assumptions about the library.  These assumptions,
  350.        though in conflict with the practice laid out by our
  351.        documentation, happened coincidentally not to cause any
  352.        errors with versions of the library shipped with IRIX 5.3
  353.        and earlier.  But it is entirely possible that some of these
  354.        mis-coded applications will fail when run with this new
  355.        release of the library.
  356.  
  357.        As the Audio File Library is a Dynamic Shared Object (DSO),
  358.        this is a matter of some concern to developers, as end-users
  359.        may upgrade OS releases and get a new DSO which breaks their
  360.        applications without the applications' developer having had
  361.        a chance to re-release.
  362.  
  363.        The library released with IRIX 6.2 is  a superset of the
  364.        previously-released library.  New functionality has been
  365.        added, but all previously-existing functions are still
  366.        present.
  367.  
  368.        This section is provided to warn developers of problems
  369.        which may be experienced with the new release due to
  370.        incorrect assumptions made in application code.
  371.  
  372.        The first problem involves assumptions about the kinds of
  373.        files the library will be able to open.  The second and
  374.        third problems have to do with using the Audio Library in
  375.        multi-threaded applications.  The fourth problem involves
  376.        the use of an AFfilehandle's file descriptor while the
  377.        filehandle is alive.  Then we follow with a warning about
  378.        the correlation between AF calls and actual UNIX operations
  379.        on files (such as read(), write(), and lseek() system
  380.        calls).
  381.  
  382.        9.7.2  _U_s_e__o_f__t_h_e__O_l_d__V_e_r_s_i_o_n__o_f__t_h_e__A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y
  383.        Because the confusion described above seems to be fairly
  384.        widespread, and because some of this confusion may have
  385.        stemmed from insufficient emphasis of these points in SGI's
  386.        documentation, the new library has been modified via the SGI
  387.        DSO versioning system so that programs built prior to the
  388.        6.2 release will still bind to a copy of the old library.
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                                   - 7 -
  401.  
  402.  
  403.  
  404.        This means that old AF programs will not be able to get the
  405.        feature and performance improvements of the new Audio File
  406.        Library without relinking.  But this will also decrease the
  407.        chances that bugs in developers' application code such as
  408.        the ones described below will be exposed, until the
  409.        developer has a chance to repair their application, relink
  410.        to bind to the new DSO, and ship a new version to their
  411.        customers.
  412.  
  413.        We did this version change as a one-time aid for developers.
  414.        We do not intend to create new DSO versions for each new
  415.        audio file library upgrade.
  416.  
  417.        We give no guarantees that the old version of libaudiofile
  418.        will be maintained in the same manner as the new version, or
  419.        in any way whatsoever.  But we can guarantee that we will
  420.        not intentionally make changes to it which would cause
  421.        applications with the below pitfalls to fail.  Please note
  422.        however that some of the problems below could happen at any
  423.        time, whether libaudiofile changes or not.  Some could
  424.        surface to users as a result of a change of any software on
  425.        the system or even a change of system hardware.  So just
  426.        because we are keeping this older libaudiofile.so does not
  427.        mean developers have any guarantee whatsoever that their
  428.        programs will continue to execute fine.  All developers
  429.        should check their software for these problems and remove
  430.        them as soon as possible.
  431.  
  432.        9.7.3  _H_o_w__D_o_e_s__T_h_i_s__D_S_O__M_a_g_i_c__W_o_r_k_?  The version of the
  433.        Audio File library which has been shipping since IRIX 5.1 is
  434.        stamped with the interface version "sgi1.0".  This can be
  435.        seen by looking at the MIPS_IVERSION entry of the DSO using
  436.        "elfdump -L <library>".  Similarly, the new version of the
  437.        Audio File Library is stamped "sgi2.0".  When a program
  438.        (specifically, a NON-ABI executable) is linked under IRIX
  439.        5.x and the DSO is specified on the command line using an
  440.        argument such as -laudiofile, ld looks at the currently
  441.        available DSOs, picks the one called libaudiofile.so, and
  442.        places that DSO's name and its interface version stamp in
  443.        the executable's liblist, which can be seen with "elfdump
  444.        -Dl <executable>".
  445.  
  446.        Then at some later time, the executable is run.  This could
  447.        be before or after a new version of the Audio File Library
  448.        is installed.  The system (specifically, rld) must then find
  449.        a libaudiofile DSO to use which has a major version of 1
  450.        (the major version is the number before the decimal point in
  451.        the interface version stamp "sgi1.0").  If no new version of
  452.        the Audio File Library has been installed, the system will
  453.        immediately find libaudiofile.so, discover that it has an
  454.        interface version of "sgi1.0", and use it.  If a new version
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                                   - 8 -
  467.  
  468.  
  469.  
  470.        of the Audio File Library has been installed as
  471.        libaudiofile.so, the system will find this DSO first, it
  472.        will look and see that the version number is "sgi2.0", and
  473.        it will reject this file.  It will then look for another DSO
  474.        called libaudiofile.so.1, since it wants to find major
  475.        version 1 of this DSO.  Since we have now installed the old
  476.        DSO libaudiofile.so.1, the system will find this DSO and use
  477.        it.
  478.  
  479.        In order for an old program to bind to the new DSO, the
  480.        programmer must relink it on a system which has version 2 of
  481.        the DSO.  This way, "sgi2.0" will be stored in the entry of
  482.        program's liblist for libaudiofile.so.  An alternate way to
  483.        make an AF program bind to the new DSO instead of the old
  484.        (1.0) DSO is to set the environment variable _RLD_ARGS to
  485.        '-ignore_version libaudiofile.so', but this will affect all
  486.        AF programs executed with this environment variable set.
  487.  
  488.        DSOs and DSO versioning are covered in dso(5), ld(1),
  489.        rld(1), and other man pages.  dso(5) contains references for
  490.        more information.
  491.  
  492.        9.7.4  _P_R_O_B_L_E_M__1  Assumption that AF only reads and writes a
  493.        certain set of file and data formats.
  494.  
  495.        The AF is a library which now supports several file formats
  496.        (besides AIFF and AIFF-C) and new data formats (besides 2's
  497.        complement integer and compressed data formats).
  498.  
  499.        This is why we always stated in the old man pages that
  500.        certain formats were the "currently supported" formats (man
  501.        afIntro(3dm)), or that certain data formats or chunk
  502.        configurations were possible "from AIFF files" rather than
  503.        "from files".
  504.  
  505.        The Audio File Library has the following behaviors which may
  506.        break applications making the above assumption when a new
  507.        library is released:
  508.  
  509.        int filefmt = afIdentifyFD(fd);
  510.        int filefmt = afGetFileFormat(handle, &version);
  511.  
  512.        Applications use afIdentifyFD for two reasons:  seeing
  513.        whether the AF recognizes an audio file, and seeing
  514.        specifically which format the audio file has.  The user uses
  515.        afGetFileFormat for the latter purpose (as well as getting
  516.        the format version, if applicable).  This section covers the
  517.        pitfalls of each use separately:
  518.  
  519.        CHECK FOR RECOGNITION:
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                                   - 9 -
  533.  
  534.  
  535.  
  536.        The filefmt returned could very well be something other than
  537.        AF_FILE_AIFF or AF_FILE_AIFFC.  This version of the library,
  538.        for example, supports six formats. The application should
  539.        make no assumptions about what tokens could be returned.
  540.  
  541.        Therefore code such as:
  542.  
  543.                if (filefmt != AF_FILE_AIFF &&
  544.                   filefmt != AF_FILE_AIFFC)
  545.                   {
  546.                   printf("this file is not supported by the AF library!\n");
  547.                   exit(0);
  548.                   }
  549.  
  550.        is incorrect because it is quite reasonable for the AF to
  551.        return a value other than one found in the include file.
  552.        Instead, the code should test whether the AF did not
  553.        recognize the file format rather than testing for what it
  554.        thinks is all possible recognized formats. Here's one
  555.        example:
  556.  
  557.                if (filefmt == AF_FILE_UNSUPPORTED ||
  558.                filefmt == AF_FILE_UNKNOWN)
  559.                        {
  560.                        printf("this file is not supported by the AF library!\n");
  561.                        exit(0);
  562.                        }
  563.  
  564.        The difference here is that the code acknowledges that there
  565.        are file formats which could be returned which the coder did
  566.        not know about at the time of coding.
  567.  
  568.        GETTING THE FORMAT ITSELF
  569.  
  570.        What about programs which actually care what the file format
  571.        of a file they are opening is?  Note that this should be
  572.        rare, because in the AF, the data format of a file's track
  573.        is queried and manipulated totally independently of the
  574.        file's file format.  So only programs which actually have to
  575.        make distinctions like "AIFF vs. AIFF-C" need use
  576.        afGetFileFormat or afIdentifyFD for this purpose.
  577.  
  578.        The caveat here, just as stated above, is that the library
  579.        could easily return something other than one of the
  580.        AF_FILE_... tokens found in /usr/include/audiofile.h at the
  581.        time of compilation.  This is because the Audio File Library
  582.        is a dynamic shared object (DSO), and thus when newer
  583.        versions of the audio file library supporting new file
  584.        formats are released with new OS's, programs linked at an
  585.        earlier time will automatically pick up the new library, and
  586.        thus could see the tokens corresponding to the newly
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                                   - 10 -
  599.  
  600.  
  601.  
  602.        supported formats.
  603.  
  604.        Before you start worrying about how to handle this
  605.        situation, make sure you have to worry about the file format
  606.        at all:
  607.  
  608.        Many applications which check the filefmt should really be
  609.        checking the sampfmt and other track parameters of the
  610.        file's track.  For example, there is no reason why a program
  611.        which simply reads the audio data out of a 16-bit
  612.        AF_SAMPFMT_TWOSCOMP AIFF file would not be able to function
  613.        just as well reading that kind of data out of a NeXT/Sun
  614.        file, as long as it did not intend to read AIFF-specific
  615.        chunks out of it as well (certain miscs and insts, for
  616.        example).  Such a program has no need to call afIdentifyFD
  617.        or afGetFileFormat to get the file format.
  618.  
  619.        But for those applications that do care about the file
  620.        format, code such as this:
  621.  
  622.                switch (afIdentifyFD(fd))
  623.                {
  624.                case AF_FILE_AIFF:
  625.                        printf("This is an AIFF file.\n");
  626.                        do_aiff_thing();
  627.                        break;
  628.                case AF_FILE_AIFFC:
  629.                        printf("This is an AIFF-C file.\n");
  630.                        do_aiffc_thing();
  631.                        break;
  632.                case AF_FILE_UNKNOWN:
  633.                case AF_FILE_UNSUPPORTED:
  634.                        printf("this file is not supported by AF library!!\n");
  635.                        exit(0);
  636.                default:
  637.                        /* assume there has been some programmatic error */
  638.                        printf("bad return value from AF library!!\n");
  639.                        assert(0);
  640.                }
  641.  
  642.        should be changed to code that at least does this:
  643.  
  644.                switch (afIdentifyFD(fd))
  645.                {
  646.                case AF_FILE_AIFF:
  647.                        printf("This is an AIFF file.\n");
  648.                        do_aiff_thing();
  649.                        break;
  650.                case AF_FILE_AIFFC:
  651.                        printf("This is an AIFF-C file.\n");
  652.                        do_aiffc_thing();
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                                   - 11 -
  665.  
  666.  
  667.  
  668.                        break;
  669.                case AF_FILE_UNKNOWN:
  670.                case AF_FILE_UNSUPPORTED:
  671.                        printf("this file is not supported by AF library!!\n");
  672.                        exit(0);
  673.                default:
  674.                        printf("this program cannot handle this file format!\n");
  675.                        exit(0);
  676.                }
  677.  
  678.        Applications can now query the AF at runtime in order to
  679.        determine which file formats are supported by the version of
  680.        the AF to which the running program has bound.  In addition
  681.        to querying the token that is used to refer to each file
  682.        format, users can query for the textual name and other
  683.        characteristics of each format.  See the afQuery(3dm) man
  684.        page for a complete description of this technique.
  685.  
  686.        For many applications, getting the textual name of the file
  687.        format is the entire reason they wanted to call afIdentifyFD
  688.        or afGetFileFormat in the first place, and so such
  689.        applications can be now written so that they instantly and
  690.        automatically understand every file format which the
  691.        currently-bound Audio File Library understands, as in:
  692.  
  693.                int filefmt = afIdentifyFD(fd);
  694.                char *name = (char *) afQueryPointer(AF_QUERYTYPE_FILEFMT,
  695.                                                 AF_QUERY_NAME, filefmt, 0, 0);
  696.                printf("This is file has the %s file format.\n", name);
  697.  
  698.        So what guarantees does the developer have?
  699.  
  700.        The library will never support fewer formats.  If a user
  701.        uses an AF_FILE_ token that we ship with a sofware release,
  702.        that token and its associated file format will never become
  703.        unsupported, although it may become obsolete (and perhaps
  704.        new features will not be available for it).  So for example,
  705.        the library will never stop supporting AF_FILE_AIFF or
  706.        AF_FILE_AIFFC.
  707.  
  708.        The library will always provide the query mechanism.  We
  709.        have made every effort to make the query mechanism versatile
  710.        enough that user programs can make intelligent decisions
  711.        about how to deal with and report info on audio files of
  712.        different file formats.
  713.  
  714.        afGetSampleFormat(AFfilehandle, int track, int *sampfmt, int
  715.        *sampwidth);
  716.  
  717.        Here is the single most likely offender that could be found
  718.        in developer's programs.  Because the new AF supports
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                                   - 12 -
  731.  
  732.  
  733.  
  734.        transparent data format conversion, you may not have to use
  735.        this routine at all, and many of the below-mentioned issues
  736.        will go away.
  737.  
  738.        As the man page says, AIFF and AIFF-C files only support
  739.        AF_SAMPFMT_TWOSCOMP files.  However, as the library now
  740.        support other file formats, it is quite likely the library
  741.        may return some other sample format.  So code that says:
  742.  
  743.                AFfilehandle h = afOpenFile(....);
  744.                if (!h) return;
  745.  
  746.                afGetSampleFormat(h, AF_DEFAULT_TRACK, &sampfmt, &sampwidth);
  747.  
  748.                /* sampfmt could only be AF_SAMPFMT_TWOSCOMP--no need to check! */
  749.  
  750.                switch (sampwidth)
  751.                        {
  752.                        ... /* see below for note on this part */
  753.                        }
  754.  
  755.        is incorrect.  It is by no means guaranteed that sampfmt is
  756.        two's complement.  Nor is it guaranteed that sampwidth has
  757.        any meaning--the meaning of sampwidth depends on sampfmt.
  758.  
  759.        For example, this version of the Audio File Library can
  760.        transfer IEEE floats and IEEE doubles.  For these types,
  761.        sampwidth has no meaning, and it is ignored and unset.  The
  762.        code above would behave very unpredictably.
  763.  
  764.        Code should say:
  765.  
  766.                AFfilehandle h = afOpenFile(....);
  767.                if (!h) return;
  768.  
  769.                afGetSampleFormat(h, AF_DEFAULT_TRACK, &sampfmt, &sampwidth);
  770.  
  771.                if (sampfmt != AF_SAMPFMT_TWOSCOMP)
  772.                        {
  773.                        printf("This program can't read audio files of this "
  774.                           sample format\n");
  775.                        exit(0);
  776.                        }
  777.  
  778.                switch (sampwidth) /* now we know sampwidth is meaningful */
  779.                        {
  780.                        ... /* see below for note on this part */
  781.                        }
  782.  
  783.        Another note on sampwidth: even with AIFF files, the sample
  784.        width is not necessarily a multiple of 8.  Generally, this
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                                   - 13 -
  797.  
  798.  
  799.  
  800.        can be ignored, because audio samples which do not take up
  801.        an integral number of bytes are left-justified inside the
  802.        next larger integral number of bytes (with the remaining
  803.        bits set to 0).  But this may be an issue in code such as
  804.        the switch statement above, if it were written:
  805.  
  806.                switch (sampwidth) /* now sampwidth is meaningful */
  807.                        {
  808.                        case 8:  do_8_thing();  break;
  809.                        case 16: do_16_thing(); break;
  810.                        case 24: do_24_thing(); break;
  811.                        case 32: do_32_thing(); break;
  812.                        default:
  813.                                /* false assumption */
  814.                                printf("bad return value from AF library!\n");
  815.                                assert(0);
  816.                        }
  817.  
  818.        There are two problems here.  One is that the code assumes
  819.        the maximum size of samples in files opened by the library
  820.        is 32 bits, which is definitely not true.  The second false
  821.        assumption is that the number of bits will be a multiple of
  822.        8.  Better code would be (after checking that sampfmt is an
  823.        integer format):
  824.  
  825.                /* round sampwidth up to nearest number of bytes */
  826.                int nbytes = ( (sampwidth-1) / 8 ) + 1;
  827.                switch (nbytes)
  828.                        {
  829.                        case 1:  do_8_thing(); break;
  830.                        case 2: do_16_thing(); break;
  831.                        case 3: do_24_thing(); break;
  832.                        case 4: do_32_thing(); break;
  833.                        default:
  834.                                printf("This program can't read audio files of this "
  835.                                   sample width %d\n", sampwidth);
  836.                                exit(0);
  837.                        }
  838.  
  839.        One final thing about sample widths--don't forget that there
  840.        is a special case for reading 24-bit integer data.  Because
  841.        3-byte data is so difficult to deal with, the library
  842.        automatically stretches the data out into convenient 4-byte
  843.        quantities in a way compatible with the SGI Audio Library
  844.        (AL).  The tricky thing here is that the data is sign-
  845.        extended on the left for this special case of 17-24-bit
  846.        data, whereas the padding that goes on to handle non-
  847.        multiple-of-8 sample widths is done by zero-bit-padding on
  848.        the right.  Here are some examples which should make this
  849.        clear, for the case of AF_SAMPFMT_TWOSCOMP:
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                                   - 14 -
  863.  
  864.  
  865.  
  866.        MSB                          LSB
  867.        byte 3  byte 2  byte 1  byte 0
  868.        aaaaaaaabbbbbbbbccccccccdddddddd | Sample Format
  869.        ---------------------------------+--------------------------------
  870.                                dddddd00 | 6-bit data takes 8 bits
  871.                                      RR |  (right pad)
  872.                                         |
  873.                                dddddddd | 8-bit data takes 8 bits
  874.                                         |  (no padding)
  875.                                         |
  876.                        ccccccccdddd0000 | 12-bit data takes 16 bits
  877.                                    RRRR |  (right pad)
  878.                                         |
  879.                        ccccccccdddddddd | 16-bit data takes 16 bits
  880.                                         |  (no padding)
  881.                                         |
  882.        s<------bbbbbbbbccccccccdddd0000 | 20-bit data takes 32 bits
  883.        SSSSSSSS                    RRRR |  (left sign-extend AND right pad !)
  884.                                         |
  885.        s<------bbbbbbbbccccccccdddddddd | 24-bit data takes 32 bits
  886.        SSSSSSSS                         |  (left sign-extend)
  887.                                         |
  888.        aaaaaaaabbbbbbbbccccccccdddddd00 | 30-bit data takes 32 bits
  889.                                      RR |  (right pad)
  890.                                         |
  891.        aaaaaaaabbbbbbbbccccccccdddddddd | 32-bit data takes 32 bits
  892.                                         |  (no padding)
  893.                                         |
  894.  
  895.        Unlike file formats, it doesn't make sense to have a runtime
  896.        query of all possible sample formats.  The existence of the
  897.        new virtual audio format system described above allows
  898.        applications to specify in which format they wish to receive
  899.        data.
  900.  
  901.        int afGetCompression(AFfilehandle, int);
  902.        void afGetCompressionParams(AFfilehandle, int, int *, AUpvlist);
  903.  
  904.        Obviously, new compression methods will be supported, so the
  905.        same warnings that apply to afGetSampleFormat apply here.
  906.  
  907.        As the library transparently decompresses input data and
  908.        compresses output data, this is not such a big issue.  The
  909.        application simply must specify the virtual format desired.
  910.  
  911.        A given already-released codec in our library will not
  912.        change its native sample format, if we have specified what
  913.        that codec's native sample format is.  So, for example, if
  914.        you know the file is AF_COMPRESSION_G711_ULAW, then you can
  915.        assume the native format for that codec is
  916.        AF_SAMPFMT_TWOSCOMP, 16 because we said this in a man page.
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                                   - 15 -
  929.  
  930.  
  931.  
  932.        This is for backwards compatibility. But we do not guarantee
  933.        that all future codecs will generate 16-bit signed integer
  934.        data natively, nor do we guarantee that all future codecs
  935.        will have a single native sample format (some may be
  936.        configurable or may vary depending on what kind of data was
  937.        originally compressed).  In every case though, the
  938.        programmer can call afGetSampleFormat to retrieve the sample
  939.        format for the current codec on the file corresponding to
  940.        the given AFfilehandle.
  941.  
  942.        This new version of the library has a runtime query of all
  943.        of the supported compression methods, their textual names,
  944.        their compression ratios (if available), and any other info
  945.        that may be useful in making a runtime choice of codec.
  946.        This is done via afQuery(3dm) in a similar manner to the
  947.        file format name string query above.
  948.  
  949.        int afGetChannels(AFfilehandle, int/*track*/);
  950.  
  951.        May return any number of channels, just like it could even
  952.        with AIFF and AIFF-C.  The virtual number of channels may be
  953.        set to any nonzero value.
  954.  
  955.        double afGetRate(AFfilehandle, int/*track*/);
  956.  
  957.        May return any sampling rate, just like it could even with
  958.        AIFF and AIFF-C.  Future versions of the library may provide
  959.        transparent conversion mechanisms which allow the programmer
  960.        to deal with files with unexpected sampling rates in a
  961.        simple and clean way.
  962.  
  963.        For now the program should either check that the sampling
  964.        rate of a file opened for reading is one the program can
  965.        deal with, or use the newly- available rate conversion
  966.        routines available in the libdmedia library.  See the
  967.        _d_m_A_u_d_i_o_R_a_t_e_C_o_n_v_e_r_t_e_r_C_r_e_a_t_e(3dm) man page and its associated
  968.        man pages for detailed information.
  969.  
  970.        int afGetTrackIDs(AFfilehandle, int */*track ids*/);
  971.  
  972.        May return more than one track.  We give no guarantees that
  973.        only one-track formats are supported.  An application might
  974.        want to reject such a file, or perhaps use the first track.
  975.        We do not define the correct behavior.
  976.  
  977.        int afGetMarkIDs(AFfilehandle, int/*track*/, int */*ids*/);
  978.        int afGetMarkPosition(AFfilehandle, int/*track*/, int/*markid*/);
  979.        char *afGetMarkName(AFfilehandle, int, int);
  980.  
  981.        May return any configuration or number of marks just as it
  982.        may with an AIFF/AIFF-C file.
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                                   - 16 -
  995.  
  996.  
  997.  
  998.        Apps should be written to expect and ignore marks they do
  999.        not understand.
  1000.  
  1001.        int afGetMiscIDs(AFfilehandle, int * /*idlist*/);
  1002.        int afGetMiscType(AFfilehandle, int /*miscid*/);
  1003.        int afGetMiscSize(AFfilehandle, int /*miscid*/);
  1004.  
  1005.        May return ANY type of misc chunk.
  1006.  
  1007.        Apps should be written to expect and ignore miscellaneous
  1008.        chunks they do not understand.
  1009.  
  1010.        Apps should be especially careful not to copy misc chunks
  1011.        from one file to another unless they understand the content
  1012.        of those misc chunks;  the chunk may contain references to
  1013.        other parts of the file which the application has modified.
  1014.        In this case the chunk in the new file becomes corrupt.
  1015.        This is to be avoided.  The chunk should not be copied and
  1016.        the user should probably be warned.
  1017.  
  1018.        int afGetAESChannelData(AFfilehandle,int,unsigned char buf[24]);
  1019.  
  1020.        Will always return data that is to be interpreted as AES
  1021.        channel data, but there is never any guarantee that such
  1022.        data will be present or not in a given file format (unless
  1023.        that file format itself defines AES data as always being
  1024.        present).
  1025.  
  1026.        int afGetInstIDs(AFfilehandle, int */*inst ids*/);
  1027.        long afGetInstParamLong(AFfilehandle, int/*inst*/, int /*param*/);
  1028.  
  1029.        May return more than one inst, unlike the one fixed INST in
  1030.        AIFF/AIFF-C files.  There are file formats which will likely
  1031.        be supported in the future which have different inst
  1032.        configurations than AIFF/AIFF-C.
  1033.  
  1034.        In the case of INST parameters it makes sense to be able to
  1035.        query at runtime the parameters of a given file format.  In
  1036.        a future release, this will be supported with afQuery()
  1037.        mechanism.
  1038.  
  1039.        Apps should, at the very least, be written to expect and
  1040.        ignore instrument configurations or instrument parameters
  1041.        they do not understand.
  1042.  
  1043.        int afGetLoopIDs(AFfilehandle, int /*inst*/, int*);
  1044.        int afGetLoopMode(AFfilehandle, int/*inst*/, int /*loopid */);
  1045.        int afGetLoopStart(AFfilehandle, int/*inst*/, int/*loopid */);
  1046.        int afGetLoopEnd(AFfilehandle, int/*inst*/, int/*loopid*/);
  1047.        int afGetLoopTrack(AFfilehandle, int/*inst*/, int/*loopid*/);
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                                   - 17 -
  1061.  
  1062.  
  1063.  
  1064.        May return any configurations of loops within an inst, not
  1065.        just the fixed value of 2 we have in AIFF/AIFF-C files.
  1066.        There are file formats which will likely be supported in the
  1067.        future which have different loop configurations than
  1068.        AIFF/AIFF-C.
  1069.  
  1070.        Some form of runtime query as to the possible loop
  1071.        configurations for a given file format may be made available
  1072.        via the afQuery() mechanism.
  1073.  
  1074.        At any rate, a program should be written to expect and
  1075.        ignore loop configurations they do not understand.
  1076.  
  1077.        9.7.5  _P_R_O_B_L_E_M__2  Some developers are writing bad multi-
  1078.        threaded code that just happens to work with the AF library.
  1079.  
  1080.        We have caught several different apps trying to do the
  1081.        following from multi-threaded code and we imagine that there
  1082.        are other apps that do this as well:
  1083.  
  1084.        ======== Thread 1 ==============|=========== Thread 2 ===========
  1085.              |                         |          |
  1086.              | some amount of time     |          | some amount of time
  1087.              |  no semaphore locking   |          | no semaphore locking
  1088.              |                         |          |
  1089.                                        |
  1090.        afSeekFrame(h,track,place1);    |    afSeekFrame(h,track,place2);
  1091.        afReadFrames(h,track,...);      |    afReadFrames(h,track,...);
  1092.                                        |
  1093.              |                         |          |
  1094.              | some amount of time     |          | some amount of time
  1095.              |  no semaphore locking   |          | no semaphore locking
  1096.              |                         |          |
  1097.  
  1098.        Both threads are making calls on the same AFfilehandle at
  1099.        the same time, and no concern is put into the order in which
  1100.        these calls end up being made on the filehandle.
  1101.  
  1102.        This code is just plain old flat out wrong, it doesn't
  1103.        matter if you're using the AF or any other library.
  1104.  
  1105.        It is entirely possible that the order the calls get
  1106.        executed in by the UNIX scheduler is:
  1107.  
  1108.        afSeekFrame(h,track,place1);    |
  1109.                                        |    afSeekFrame(h,track,place2);
  1110.        afReadFrames(h,track,...);      |
  1111.                                        |    afReadFrames(h,track,...);
  1112.  
  1113.        in which case both threads would read the wrong data. UNIX
  1114.        offers absolutely, positively no guarantee that this will
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                                   - 18 -
  1127.  
  1128.  
  1129.  
  1130.        not happen unless the programmer uses one of the many
  1131.        available process coordination facilities such as
  1132.        semaphores.
  1133.  
  1134.        Furthermore, no amount of work on the part of the AF library
  1135.        writers can prevent this situation, because the library has
  1136.        no knowledge of what order the programmer really wants the
  1137.        operations to come in.  We cannot use the pid as a hint
  1138.        because some correctly coded programs may want to seek in
  1139.        one thread and read in another (correctly coded programs use
  1140.        locking to make sure that these operations happen in the
  1141.        right order).
  1142.  
  1143.        Note that although our example only uses the functions
  1144.        afSeekFrame() and afReadFrames(), this problem can exist
  1145.        with calls to ANY Audio File Library functions, even ones
  1146.        which do not directly manipulate audio files (such as
  1147.        operations on an AFfilesetup or operations on an
  1148.        AFfilehandle that query or set parameters stored in memory).
  1149.        In particular, watch out for uncoordinated calls of
  1150.        afSyncFile() and afCloseFile().  Making these calls from
  1151.        more than one thread simultaneously in an uncoordinated
  1152.        fashion seems to be a common error.
  1153.  
  1154.        Correct code should look like this:
  1155.  
  1156.        ======== Thread 1 ==============|=========== Thread 2 ===========
  1157.                                        |
  1158.              |                         |          |
  1159.              | some amount of time     |          | some amount of time
  1160.              |                         |          |
  1161.              |                         |          |
  1162.                                        |
  1163.        Lock Sempaphore that guards h   |    Lock Semaphore that guards h
  1164.        afSeekFrame(h,track,place1);    |    afSeekFrame(h,track,place2);
  1165.        afReadFrames(h,track,...);      |    afReadFrames(h,track,...);
  1166.        Unlock Semaphore that guards h  |    Unlock semaphore that guards h
  1167.                                        |
  1168.              |                         |          |
  1169.              | some amount of time     |          | some amount of time
  1170.              |                         |          |
  1171.              |                         |          |
  1172.                                        |
  1173.  
  1174.        The key thing to realize here is: UNIX guarantees that only
  1175.        one of the Lock Semaphore calls will succeed immediately.
  1176.        The thread whose lock does not succeed will sit and wait in
  1177.        the Lock Semaphore call (and thus not proceed to the
  1178.        afSeekFrame) until the other thread has unlocked the
  1179.        semaphore (after doing its seek AND its read).  Then when
  1180.        the other thread unlocks the semaphore, the thread who was
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                                   - 19 -
  1193.  
  1194.  
  1195.  
  1196.        forced to wait will now proceed.
  1197.  
  1198.        How does one use these semaphores?  One uses code such as
  1199.        the following to create a semaphore FileOpSema with value 1:
  1200.  
  1201.        #include <ulocks.h>
  1202.  
  1203.        AFfilehandle h;      /* global file handle */
  1204.        usema_t *FileOpSema; /* global semaphore to protect h */
  1205.  
  1206.        /* initialize semaphore support -- do this once */
  1207.            {
  1208.            usptr_t *usptr;
  1209.            char *arenafile;
  1210.  
  1211.            /* use fastest (no debugging) form of semaphores -- usconfig (3P) */
  1212.            usconfig(CONF_LOCKTYPE, US_NODEBUG);
  1213.  
  1214.            /* create shared arena to hold the semaphore -- usinit(3P) */
  1215.            arenafile = tmpnam(NULL);
  1216.            usptr = usinit(arenafile);
  1217.  
  1218.            /*
  1219.                create the semaphore in that arena -- usnewsema(3P)
  1220.                create with count 1--there is 1 resource (h), which is
  1221.                initially available.
  1222.            */
  1223.            FileOpSema = usnewsema(usptr,1);
  1224.  
  1225.            /* we won't need to refer to arena again so we can unlink file */
  1226.            unlink(arenafile);
  1227.            }
  1228.  
  1229.        Then one uses uspsema(3P) to lock the semaphore, and
  1230.        usvsema(3P) to unlock the semaphore.  Pretty simple:
  1231.  
  1232.        ======== Thread 1 ==============|=========== Thread 2 ===========
  1233.                                        |
  1234.              |                         |          |
  1235.              | some amount of time     |          | some amount of time
  1236.              |                         |          |
  1237.              |                         |          |
  1238.                                        |
  1239.        uspsema(FileOpSema); /*lock*/   |    uspsema(FileOpSema); /*lock*/
  1240.        afSeekFrame(h,track,place1);    |    afSeekFrame(h,track,place2);
  1241.        afReadFrames(h,track,...);      |    afReadFrames(h,track,...);
  1242.        usvsema(FileOpSema); /*unlock*/ |    usvsema(FileOpSema); /*unlock*/
  1243.                                        |
  1244.              |                         |          |
  1245.              | some amount of time     |          | some amount of time
  1246.              |                         |          |
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                                   - 20 -
  1259.  
  1260.  
  1261.  
  1262.              |                         |          |
  1263.                                        |
  1264.  
  1265.        Now, why are we pointing this out for AF users?
  1266.  
  1267.        We have found several applications which were actually
  1268.        committing this error and were GETTING AWAY WITH IT!  By the
  1269.        most arcane of coincidences of CPU scheduler timings, these
  1270.        apps just happened to never fall into the short window of
  1271.        time in which the above worst-case scenario could occur.
  1272.  
  1273.        When these programs were recompiled with an alpha version of
  1274.        a newer Audio File Library, which contains different code
  1275.        and thus has slightly different timing characteristics and
  1276.        gets scheduled slightly differently, the bug in these
  1277.        applications was instantly revealed.
  1278.  
  1279.        This is why we want to alert developers to check their code
  1280.        for this-- there could be a bug of this type in their code
  1281.        which had previously not come out, just because the old AF
  1282.        happens to have had this property (by no design of our own).
  1283.        We want to give developers plenty of advanced warning about
  1284.        this situation.
  1285.  
  1286.        9.7.6  _P_R_O_B_L_E_M__3  Some developers are writing multi-threaded
  1287.        code that would be good under their assumption that the AF
  1288.        is MT/MP-safe. But this assumption is false.
  1289.  
  1290.        The AF is NOT a multi-thread and/or multi-processor safe
  1291.        library, in the following sense:
  1292.  
  1293.        Users can make multiple, simultaneous, uncoordinated AF
  1294.        calls on different AFfilehandles from different threads and
  1295.        the library will operate fine.  Each AFfilehandle completely
  1296.        encapsulates the state needed to do operations on that
  1297.        AFfilehandle (except for error handling, which is explained
  1298.        next).
  1299.  
  1300.        Users cannot make multiple, simultaneous, uncoordinated AF
  1301.        calls from different threads to set or access the library's
  1302.        global state--namely, the error handler function.  If two
  1303.        threads simultaneously try and set the error handler (even
  1304.        the same error handler), the behavior is undefined.  We will
  1305.        likely add a form of MT-safe error handling mechanism in a
  1306.        future release of the AF.  For example, we may add the
  1307.        concept of an error handler that is thread-specific.  But we
  1308.        cannot offer guarantees that we will provide such a
  1309.        mechanism.
  1310.  
  1311.        Furthermore, if the user writes an error handler, then makes
  1312.        multiple, simultaneous, uncoordinated AF calls on different
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                                   - 21 -
  1325.  
  1326.  
  1327.  
  1328.        filehandles from different threads, and both AF calls issue
  1329.        an error simultaneously, then two instances of the user's
  1330.        error handler will be called in a simultaneous,
  1331.        uncoordinated manner in two threads.  If this situation is
  1332.        possible in a user's program, the user should use semaphores
  1333.        in their error handler in order to make sure their handler
  1334.        doesn't try and report or deal with two errors at the same
  1335.        time.  Note that any AF function can cause an AF error to
  1336.        occur.  Do not assume a function will not err just because
  1337.        it is simple.
  1338.  
  1339.        Now the most important caveat: users cannot make multiple,
  1340.        simultaneous, uncoordinated AF calls on the SAME
  1341.        AFfilehandle from different threads, even if the order of
  1342.        execution of those calls does not matter to the user.  Doing
  1343.        so will very likely cause a core dump, or at least
  1344.        corruption of the AFfilehandle.  This behavior will never be
  1345.        changed, as we refuse to make our developers pay the price
  1346.        of semaphore locking code at the beginning and end of every
  1347.        afReadFrames and afWriteFrames call.  Most users do not
  1348.        need, and in fact really do not want, semaphore protection
  1349.        that is built-in to the AF calls themselves.
  1350.  
  1351.        Like problem 2, problem 3 can occur with any AF call, not
  1352.        just afSeekFrame() and afReadFrames().  In particular, watch
  1353.        out for simultaneous calls of afSyncFile() and
  1354.        afCloseFile().
  1355.  
  1356.        Note that at no point did we offer any guarantee that the
  1357.        library was MT/MP-safe, and in fact only libraries specified
  1358.        as such in man pages (such as the C library minus errno, as
  1359.        explained in intro(3)) are guaranteed to be so.
  1360.  
  1361.        9.7.7  _P_R_O_B_L_E_M__4  Some developers are using afOpenFD() or
  1362.        afGetFD() to get the file descriptor which an AFfilehandle
  1363.        is using, and then changing the file pointer on that file
  1364.        descriptor.  The AF does not allow this.
  1365.  
  1366.        The file descriptor returned by afGetFD() was intended to be
  1367.        used as part of a select() loop and was not intended to
  1368.        allow users to read, write, and seek in the file without the
  1369.        knowledge of the Audio File Library.  Doing so will cause
  1370.        the library to give unpredictable results unless the user
  1371.        saves and restores the file position whenever they modify
  1372.        it.
  1373.  
  1374.        The reason for this is that we are not willing to force upon
  1375.        users expecting high performance the often very large
  1376.        overhead of an lseek(2) system call on every afReadFrames()
  1377.        or afWriteFrames().  Therefore we cannot restore the file
  1378.        position to a known place on every AF call which relies on
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                                   - 22 -
  1391.  
  1392.  
  1393.  
  1394.        the file being in that place.
  1395.  
  1396.        Users who wish to modify the file position of the file
  1397.        descriptor given by afGetFD() should save the file position
  1398.        and restore it after they are finished and before they make
  1399.        the next AF call to read or write data to the file.  The new
  1400.        routines _a_f_S_a_v_e_F_i_l_e_P_o_s_i_t_i_o_n(3dm) and
  1401.        _a_f_R_e_s_t_o_r_e_F_i_l_e_P_o_s_i_t_i_o_n(3dm) have been created expressly for
  1402.        this purpose.  Alternately they can use one of two different
  1403.        file descriptors opened to the same file (dup(2) will not
  1404.        work; it gives you two file descriptors which share a file
  1405.        offset.  The file must be re-opened in order to get a
  1406.        separate file descriptor).  Also, if users attempt to write
  1407.        to the file, no matter how the AFfilehandle was opened, the
  1408.        results are undefined.
  1409.  
  1410.        Developers had asked for a call to get the offset of the
  1411.        audio data in an audio file.  Two new functions have been
  1412.        made available.  afGetDataOffset(3dm) returns the offset in
  1413.        bytes from the beginning of an open audio file to the first
  1414.        audio sample.  afGetTrackBytes(3dm) returns the size in
  1415.        bytes of the audio data portion of a given track in an audio
  1416.        file.
  1417.  
  1418.        9.7.8  _W_A_R_N_I_N_G  We give no guarantees about the number or
  1419.        nature of UNIX system calls that will result from a given AF
  1420.        call.  In particular, afReadFrames() and afWriteFrames()
  1421.        could actually read() or write() any amount of data in the
  1422.        file, or could read() or write() more than once in varying
  1423.        chunk sizes.  Also, afOpenFile(), afSeekFrame(),
  1424.        afSyncFile(), afCloseFile(), and other AF functions could
  1425.        result in any amount of data being read from or written to
  1426.        the file.  (but of course the AF will not write to a file
  1427.        opened for read access or read from a file opened for write
  1428.        access).
  1429.  
  1430.        Users who are attempting to optimize the I/O in their
  1431.        program by managing I/O system call behavior should be aware
  1432.        that at this time we offer no guarantees about when the AF
  1433.        will perform system calls.
  1434.  
  1435.        This has not been a problem to our knowledge, but we thought
  1436.        it would be a good idea just to make this explicit.  At this
  1437.        time we offer no guarantees that a read of, say, 50,000
  1438.        frames will result in a single UNIX read() system call for
  1439.        exactly 50,000 frames worth of data.  Although we will
  1440.        strive to make this the case whenever it seems beneficial,
  1441.        there are certain cases where this is simply impossible.
  1442.        For example, reads from or writes to a compressed file will
  1443.        generally be split up into a chunksize which is natural for
  1444.        the codec being used, and thus possibly result in several
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                                   - 23 -
  1457.  
  1458.  
  1459.  
  1460.        reads or writes to the file.  Also, we give no guarantee
  1461.        that afSeekFrame() will seek the file at all in the UNIX
  1462.        lseek() sense, although it happens that this is generally
  1463.        the case for the current Audio File Library.  And even if
  1464.        afSeekFrame() does lseek() the file, we do not guarantee
  1465.        that it will be to the exact frame the user specified
  1466.        (though this is generally the case in the old AF library).
  1467.        This is due to necessary compensation for filter delays
  1468.        inherent in certain forms of compression and decompression.
  1469.  
  1470.        In a future release, features will be introduced which may
  1471.        make this more of an issue.  At that time we will come up
  1472.        with much more specific information about what guarantees we
  1473.        can offer to the developer.  We want to find out if these
  1474.        kinds of guarantees are something that developers need at
  1475.        all.  Perhaps it is simply not an issue for developers.  But
  1476.        if it is, we would be most interested in hearing your
  1477.        feedback.  You can contact your local SGI representative and
  1478.        ask them to send a message to the developers of the Audio
  1479.        File Library, or you can also use the newsgroup
  1480.        comp.sys.sgi.audio for faster and much more direct contact.
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.